package com.twu.biblioteca.controllers;

import com.twu.biblioteca.models.Book;
import com.twu.biblioteca.interfaces.IBookProvider;

/**
 * Created with IntelliJ IDEA.
 * User: mmoise
 * Date: 14/05/2013
 * Time: 12:00
 * To change this template use File | Settings | File Templates.
 */
public class BookController {

    IBookProvider bookProvider;
    private Book[] books;

    public BookController(IBookProvider bookProvider){
        this.bookProvider = bookProvider;
        books = bookProvider.getAllBooksInTheLibrary();
    }

    public Book[] getAllBooks() {
        return books;
    }

    public Book findBookByCode(String code) {
        Book book = null;
        for (int i=0;i<books.length;i++){
            if (books[i].getCode().equals(code)) book = books[i];
        }
        return book;
    }

    public boolean reserveBookWithCode(String code) {
        Book book = findBookByCode(code);
        if ((book!=null) && (!book.isReserved()))  { book.reserve(); return true;}
        else return false;
    }
}
